﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using H2.NET.Web.Workflow.DwzCode;
using H2.NET.Web.Workflow.Services;
using H2.NET.Common;

namespace H2.NET.Web.Management.Account
{
    public partial class Index : BasePage
    {
        private readonly AccountService _accountService = new AccountService();

        public string Username;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (AksRequest.GetQueryString("action") == "delete")
            {
                var id = AksRequest.GetFormInt("id");

                var account = _accountService.GetModel(id);
                if (account.Username == "*")
                {
                    ErrorMsg("对不起，系统帐号不能删除");
                    return;
                }

                _accountService.Delete(id);

                SuccessMsg("AccountIndex");
                return;
            }

            string strWhere = "1=1";
            if (!string.IsNullOrEmpty(Request.Form["Username"]))
            {
                strWhere += " and Username like '%" + Request.Form["Username"].Trim() + "%'";
            }

            TotalItemCount = _accountService.GetRecordCount(strWhere);
            PageNumber = Convert.ToInt32(Request.Form["pageNum"] ?? "1");

            Pager1.TotalItemCount = TotalItemCount;
            Pager1.PageSize = PageSize;
            Pager1.PageNumber = PageNumber;
            rpt_users.DataSource = _accountService.GetListByPage(strWhere, "",
                                                              PageSize * (PageNumber - 1) + 1,
                                                              PageSize * PageNumber);
            rpt_users.DataBind();


            Username = Request.Form["Username"];
        }

        protected void rpt_users_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var lbStatus = e.Item.FindControl("l_status") as Literal;
                var l_role = e.Item.FindControl("l_role") as Literal;
                if (lbStatus == null) return;

                if (lbStatus.Text == "True")
                {
                    lbStatus.Text = "<a href='javascript:;' class='table_btn status_on'></a>";
                }
                else
                {
                    lbStatus.Text = "<a href='javascript:;' class='table_btn status_off'></a>";
                }

                var role_id = Utils.StrToInt(l_role.Text, 0);
                var roleService = new RoleService();
                var result = roleService.FindRoleByAccountId(role_id);
                var str = result.Rows.Cast<DataRow>()
                    .Aggregate(string.Empty, (current, row) => current + (row["name"] + ","));
                l_role.Text = str;
            }
        }
    }
}